package com.zlx.mall.basic.util.net;

import org.apache.commons.net.util.SubnetUtils;
import org.springframework.util.StringUtils;

/**
 * @author qingke
 **/
public final class SubnetUtil {

    private static final String COMMA= ",";

    public static boolean checkAllowAddress(String allowAddress, String remoteAddress) {
        if (StringUtils.hasText(allowAddress) && StringUtils.hasText(remoteAddress)) {
            for (String address : StringUtils.tokenizeToStringArray(allowAddress, COMMA)) {
                address = address.trim();
                if (address.indexOf('/') == -1) {
                    if (address.equals(remoteAddress)) {
                        return true;
                    }
                } else {
                    SubnetUtils subnetUtils = new SubnetUtils(address);
                    if (subnetUtils.getInfo().isInRange(remoteAddress)) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}
